/*
 *  Copyright (C) 2009 Proyecto HORUS - CSIC
 *  http://horus.ebd.csic.es
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 2.1 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.

 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version:		0.0.1
 *  Design:		Manuel Baena
 *  Implementation:	Manuel Baena
 */


#include "HorusClasses.h"

int bloqueo=0;

Balanza::Balanza()
{
 
}

uint8_t Balanza::getMode(char *mensaje)
{
	  uint8_t resultado=2;

	  if(mensaje[0]=='S' && mensaje[1]=='T'){
		resultado=0;
	  }

	  if(mensaje[0]=='U' && mensaje[1]=='S'){
	  	resultado=1;
	  }

	  return resultado;
}

float Balanza::getValue(char *mensaje)
{
	  float resultado;
	  int signo;
	  uint16_t entero;
	  uint8_t decimal;

	  if (mensaje[6] == '-'){
		signo=-1;
	  }else{
		signo=1;
	  }
	 
	  if(mensaje[8]==' '){
		
		if(mensaje[9]==' '){
			entero=itoil(mensaje[10]);
		}else{
			entero=itoil(mensaje[9])*10+itoil(mensaje[10]);
		}
	  }else{
		entero=itoil(mensaje[8])*100+itoil(mensaje[9])*10+itoil(mensaje[10]);
	  }
	  decimal=itoil(mensaje[12])*10+itoil(mensaje[13]);
	  resultado=entero+decimal*0.01;


	return signo*resultado;
}



/*
 * getRaw (int) - gets a data string from the balance
 *
 * store the next consisten message in the internal buffer Utils.inBuffer
 * if the input is 0, continue until the end of the message or up to
 * the max buffer size (BALANCE_BUFFER_SIZE).
 *
 * It will return the string and not only update it in the Utils.inBuffer.
 *
 */
char* Balanza::getRaw()
{	
  uint8_t byteBalance = 0;		// the last byte coming through the port
  uint8_t i = 2;			    // count the amount of bytes read
  bool start = false;       // var to wait date of start
  bloqueo=0;

 // if (byteAmount == 0) byteAmount = GPS_BUFFER_SIZE;

  Utils.clearBuffer();
 
  Serial.println("--1--");
  byteBalance = Serial2.read();	// read the first byte coming through the port
  Serial.println("--2--");
//Serial.println(byteBalance);
//Serial.println(byteBalance);
  while(!start){
    
      Serial.println("--3--");


	  if (byteBalance == 'S')
	  {       //Serial.println(byteBalance); 
		  byteBalance = Serial2.read();	// flush incomplete sentences
		  //Serial1.flush();	
		  //Serial.print(byteBalance);
		  if (byteBalance == 'T'){
		      //Serial.println(byteBalance);
		      Utils.inBuffer[0]='S';
		      Utils.inBuffer[1]=byteBalance;
		      start = true;
		  }

	  }else if (byteBalance == 'U'){
		  //Serial.println(byteBalance);
		  byteBalance = Serial2.read();	// flush incomplete sentences
		  //Serial1.flush();		 
		  //Serial.print(byteBalance);
   	      if (byteBalance == 'S'){
		     // Serial.println(byteBalance);
   	    	      Utils.inBuffer[0]='U';
		      Utils.inBuffer[1]=byteBalance;
		      start = true;
		  }
	  }else if (byteBalance == 'O'){
		  //Serial.println(byteBalance);
		  byteBalance = Serial2.read();	// flush incomplete sentences
		  //Serial1.flush();		 
		  //Serial.print(byteBalance);
   	      if (byteBalance == 'L'){
		     // Serial.println(byteBalance);
   	    	      Utils.inBuffer[0]='O';
		      Utils.inBuffer[1]=byteBalance;
                      for(int j=2;j<20;j++){
                          
                          Utils.inBuffer[j]=' ';
                      }
		      start = true;
                      i=20;
		  }
          }else{
            
                  Serial.println("--4--");
		  if(Serial2.available()>0 ){
  
        Serial.println("--5--");
		  	byteBalance = Serial2.read(); // flush incomplete sentences
			//Serial.println(byteBalance);
			Serial2.flush();
                        start = true;
                        i=20;
                        for(int j=0;j<20;j++){
                          
                          Utils.inBuffer[j]='E';
                        }
		  }else{
  
                      bloqueo++;
                      if(bloqueo>1000){
                      start=true;


                      }
  
        Serial.println("--6--");
                              Serial.println(bloqueo);
  
  
  
                  }
                       
	  }

  }
  
          Serial.println("--7--");
  //Serial.println("aqui3");
  //Serial.flush();
  while(i < 20 && bloqueo<1000){          // read the Balance message
//  while(!i || (byteBalance != '*' && byteBalance != '$' && i < byteAmount)){ // read the Balance message
          Serial.println("--8--");
    if(Serial2.available()>0)
    {
                Serial.println("--9--");
      byteBalance = Serial2.read();
                Serial.println("--10--");
      
      //Serial.print(byteBalance);
     // Serial1.flush();
//      if (byteBalance != '*' && byteBalance != '$' && i < byteAmount) Utils.inBuffer[i]=byteBalance;
//	if(temp!=-1){
            Serial.println("--11--");
      
		Utils.inBuffer[i]=byteBalance;
      		i++;
      
                Serial.println("--12--");
		//Serial.println(i);
		//Serial.print(byteBalance);
  		//
//	}
    }
    
              Serial.println("--13--");
	
  
  }
  Serial2.flush();
  delay(50);
  
  return Utils.inBuffer;
}


 /*
  * atol (str) - get the integer part of a number out of a string
  *
  * It gets the integer part of a number out of a string
  */
 int Balanza::itoil(char str)
 {
 	int ret = str++ - '0';
 	return ret;
 }

// Preinstantiate Objects //////////////////////////////////////////////////////

Balanza Peso = Balanza();





